SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE  Procedure [dbo].[SP_TDCONC_FLUJ_Q01]
/*---------------------------------------------------------*/
/*---- Empresa		   : OFISIS S.A				----*/
/*---- Cliente		   : OFISIS S.A.				----*/
/*---- Sistema		   : Tesoreria					----*/
/*---- Modulo		   : Empresa					----*/
/*---- Programa	   : Flujo de Caja Proyectado, Eje	----*/
/*---- Script		   : TDCONC_FLUJ_Q01.SQL			----*/
/*---- Desarrollado por	   : Carlos Marquez Escobar	----*/
/*---- Fecha Creacion	   : 03/03/2004			----*/
/*---- Base de Datos	   : SQL SERVER			----*/
/*---- Version		   : 8.0						----*/
/*---- Invoca a SP 	   :							----*/
/*---------------------------------------------------------*/
/*-- Comentarios	   : 						
CREATE TABLE #TWCONC_FLUJ (
	CO_EMPR		Varchar(002)	Null,
	TI_CLIE		Varchar(003)	Null,
	FE_PROY_EJEC	Datetime		Null, 
	ST_DIGI_PROY	Varchar(001)	Null,
	CO_MONE		Varchar(003)	Null, 
	FE_DETA_DIGI	Datetime		Null, 
	DE_SUBR		Varchar(100)	Null,
	IM_SALD		Numeric(16,4)	Null,
	IM_DIGI		Numeric(16,4)	Null,
	IM_EJEC		Numeric(16,4)	Null)			----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/* drop proc SP_TDCONC_FLUJ_Q01  					----*/ 
/* grant all on SP_TDCONC_FLUJ_Q01 to PUBLIC			----*/
/* SP_TDCONC_FLUJ_Q01  '01', 'FTEERFA', 'DOL', '2004-01-01', 3.5   ----*/
/*---------------------------------------------------------*/
(
@ISCO_EMPR     	TD_VC_020,
@ISCO_MENU     	TD_VC_008,
@ISCO_MONE     	TD_VC_003,
@IDFE_PROY     	TD_DT_001,
@INFA_TIPO_CAMB	TD_NU_015_006

)
	As
Declare
	@CSCO_EMPR     	TD_VC_020,
	@CDFE_DETA_DIGI TD_DT_001,
	@CSNU_DIAS     	TD_VC_003,
	@CSDE_DIAS     	TD_VC_100,

	@VSDE_SELE_DIAS Varchar(5000),


	@VSFE_PROY		TD_VC_020,
	@VSTI_CLIE		TD_VC_003,
	@VNNU_ANNO		TD_IN_001,
	@VNNU_MESE		TD_IN_001,
	@VNNU_ACCI		TD_IN_001,
	@VSCO_MONE_DEFA	TD_VC_003,
	@VSCO_MONE_EXTR	TD_VC_003,
	@VSCO_TIAU_CCOS	TD_VC_001,
	@VSCO_TIAU_CLIE	TD_VC_001,
	@VSCO_TIAU_PROV	TD_VC_001,
	@VSCO_TIAU_EMPL	TD_VC_001,
	@VSTI_CAMB		TD_VC_003,
	@VSTI_ORIG		TD_VC_003,
	@VNNU_DIAS		TD_IN_001,
	@VNNU_DIAS_COUNT	TD_IN_001,
	@VDFE_DIAS_AUXI	TD_DT_001,
	@VDFE_MINI_DIAS	TD_DT_001,
	@VDFE_DIAS_MESA	TD_DT_001
-------------------------------------------------------------------------------------------------
Begin
	--- *** CREACION DE LA TABLA TEMPORAL EN DONDE SE ALMACENARAN LOS DATOS FINALES *** ---
	CREATE TABLE #TWCONC_FLUJ_Q01_1
	(
	CO_EMPR		Varchar(002)	Null,
	TI_CLIE		Varchar(003)	Null,
	FE_PROY_EJEC	Datetime		Null, 
	CO_MONE		Varchar(003)	Null, 
	IM_ORIG		Numeric(16,4)	Null,
	IM_CONV		Numeric(16,4)	Null,
	ST_EJEC		Varchar(001)	Null
	)

-------------------------------------------------------------------------------------------
--- *** OBTINE MONEDA NACIONAL Y EXTRAJERA, TIPO DE CAMBIO x PANTALLA O PARAMETROS *** ---	
	Select 	@VSCO_MONE_DEFA = t1.CO_MONE_DEFA, @VSCO_MONE_EXTR = t1.CO_MONE_EXTR,
			@VSTI_CAMB = IsNull(t2.TI_CAMB,t1.TI_CAMB), @VSTI_ORIG = IsNull(t2.TI_ORIG, t1.TI_ORIG),
			@VSCO_TIAU_PROV = t1.CO_TIAU_PROV,
			@VSCO_TIAU_CLIE = t1.CO_TIAU_CLIE,
			@VSCO_TIAU_CCOS = t1.CO_TIAU_CCOS,
			@VSCO_TIAU_EMPL = t1.CO_TIAU_EMPL
	From 	TMPARA_TESO t1
	Left Outer Join	TTFACA_OPCI t2
	On	t1.CO_EMPR = t2.CO_EMPR
	And 	t2.CO_MODU  = 'TEEM0001' 
	And	t2.CO_MENU  = @ISCO_MENU 
	Where 	t1.CO_EMPR  = @ISCO_EMPR 
	 
	



--- *** OBTINE EL NUMERO DE DIAS PARA GENERAR DIAS EN BLANCO VS RUBRO*** ---
	---select 	@VDFE_DIAS_AUXI = @INFA_TIPO_CAMB

	
	/*---------------------------------------------------------*/
	/*--------- P  R  O  Y  E  C  T  A  D  O  --------  1   ---*/
	/*---------------------------------------------------------*/
	--- *** 1.1.- INSERTO TABLA TEMPORAL PROYECTADO DOCS CLIENTES *** ---		
	Insert Into #TWCONC_FLUJ_Q01_1
	(CO_EMPR,	TI_CLIE,	FE_PROY_EJEC, CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	(Select	t1.CO_EMPR, t1.TI_CLIE,	t1.FE_VENC, t1.CO_MONE,
			 ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ) , 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t1.CO_MONE,  @ISCO_MONE, t1.FE_VENC, ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ), 
@VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, @INFA_TIPO_CAMB ) * 
			    ( PatIndex (t8.ST_SIGN, 'S') - PatIndex (t8.ST_SIGN, 'N') ),
			 '1'
	 From 	TMDOCU_CLIE t1, 
			(Select	Top 10	t1.CO_EMPR, t1.FE_VENC
			 From	TMDOCU_CLIE t1
			 Where 	t1.CO_EMPR = @ISCO_EMPR And 
					t1.FE_VENC >= @IDFE_PROY And
					t1.CO_ESTA_DOCU != 'ANU'  And
					Len(Rtrim(ltrim(t1.TI_CLIE)))> 0
			 Group By t1.CO_EMPR, t1.FE_VENC ) t2,
			TTDOCU_CNTB t8
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_VENC >= @IDFE_PROY And
			t1.CO_ESTA_DOCU != 'ANU'  And
			Len(Rtrim(ltrim(t1.TI_CLIE)))> 0 And
			t1.CO_TIPO_DOCU = t8.TI_DOCU
	 )

		Insert Into #TWCONC_FLUJ_DIAS (
		CO_EMPR,	FE_DETA_DIGI,	NU_ANNO,	NU_MESE,	DE_DIAS,	NU_DIAS, ST_ORIG)
		(Select	Top 10	t1.CO_EMPR, t1.FE_VENC, Year(t1.FE_VENC), Month (t1.FE_VENC) ,				
			 dbo.FN_DESC_DIAS_Q01(DATEPART ( dw , t1.FE_VENC )) + '_' + Convert(Varchar(2),DatePart(Day, 
t1.FE_VENC)) , 
			RTrim(Ltrim(Convert(Varchar(3),IsNull(DatePart ( dayofyear , t1.FE_VENC ), 0)))),
			  '1'
		 From	TMDOCU_CLIE t1
		 Where 	t1.CO_EMPR = @ISCO_EMPR And 
					t1.FE_VENC >= @IDFE_PROY And
					t1.CO_ESTA_DOCU != 'ANU'  And
					Len(Rtrim(ltrim(t1.TI_CLIE)))> 0
		 Group By t1.CO_EMPR, t1.FE_VENC )
	
	
	--- *** 1.2.- INSERTO TABLA TEMPORAL PROYECTADO DOCS PROVEEOR *** ---		
	Insert Into #TWCONC_FLUJ_Q01_1
	(CO_EMPR,	TI_CLIE,	FE_PROY_EJEC, CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	(Select	t1.CO_EMPR, t1.TI_PROV,	t1.FE_VENC, t1.CO_MONE,
			 ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ) , 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t1.CO_MONE,  @ISCO_MONE, t1.FE_VENC, ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ), 
@VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, @INFA_TIPO_CAMB ) * 
			    ( PatIndex (t8.ST_SIGN, 'S') - PatIndex (t8.ST_SIGN, 'N') ),
			 '2'
	 From 	TMDOCU_PROV t1, 
			(Select	Top 10	t1.CO_EMPR, t1.FE_VENC
			 From	TMDOCU_PROV t1
			 Where 	t1.CO_EMPR = @ISCO_EMPR And 
					t1.FE_VENC >= @IDFE_PROY And
					t1.CO_ESTA_DOCU != 'ANU'
			 Group By t1.CO_EMPR, t1.FE_VENC ) t2,
			TTDOCU_CNTB t8
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_VENC >= @IDFE_PROY And
			t1.CO_ESTA_DOCU != 'ANU'  And
			t1.CO_TIPO_DOCU = t8.TI_DOCU	 )


		Insert Into #TWCONC_FLUJ_DIAS (
		CO_EMPR,	FE_DETA_DIGI,	NU_ANNO,	NU_MESE,	DE_DIAS,	NU_DIAS, ST_ORIG)
		(Select	Top 10	t1.CO_EMPR, t1.FE_VENC, Year(t1.FE_VENC), Month (t1.FE_VENC) ,
			 dbo.FN_DESC_DIAS_Q01(DATEPART ( dw , t1.FE_VENC )) + '_' + Convert(Varchar(2),DatePart(Day, 
t1.FE_VENC)), 
			RTrim(Ltrim(Convert(Varchar(3),IsNull(DatePart ( dayofyear , t1.FE_VENC ), 0)))), '2'
		 From	TMDOCU_PROV t1
		 Where 	t1.CO_EMPR = @ISCO_EMPR And 
					t1.FE_VENC >= @IDFE_PROY And
					t1.CO_ESTA_DOCU != 'ANU' 
		 Group By t1.CO_EMPR, t1.FE_VENC )


	--- *** 1.3.- INSERTO TABLA TEMPORAL PROYECTADO CONCEPTOS ADICIONALES  *** ---		
	Insert Into #TWCONC_FLUJ_Q01_1
	(CO_EMPR,	TI_CLIE,	FE_PROY_EJEC, CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	(Select 
	t1.CO_EMPR, t1.CO_CONC_FLUJ,	t1.FE_REGI, t1.CO_MONE,
			 IsNull(t1.IM_DIGI,0), 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t1.CO_MONE,  @ISCO_MONE, t1.FE_REGI, IsNull(t1.IM_DIGI,0), @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, @INFA_TIPO_CAMB ),
			 '3'
	From TDCONC_FLUJ t1,
			(Select	Top 10	t1.CO_EMPR, t1.FE_REGI
			 From	TDCONC_FLUJ t1
			 Where 	t1.CO_EMPR = @ISCO_EMPR And 
					t1.CO_MONE = @ISCO_MONE  And
					t1.FE_REGI >= @IDFE_PROY
			 Group By t1.CO_EMPR, t1.FE_REGI ) t2
	Where
		t1.CO_EMPR = @ISCO_EMPR And 
		t1.CO_MONE = @ISCO_MONE  And
		t1.FE_REGI = @IDFE_PROY  And
		t1.CO_EMPR = t2.CO_EMPR  And
		t1.FE_REGI = t2.FE_REGI
	Union All
	Select 
		t1.CO_EMPR, t1.CO_CONC_FLUJ,	t1.FE_REGI, t1.CO_MONE,
				 IsNull(t1.IM_DIGI,0), 
				 dbo.FN_TCFACT_CAMB_Q02 
					(t1.CO_MONE,  @ISCO_MONE, t1.FE_REGI, IsNull(t1.IM_DIGI,0), @VSTI_CAMB, @VSTI_ORIG,
					'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, @INFA_TIPO_CAMB ),
				 '3'
	From TDCONC_FLUJ t1, 
				(Select	Top 10	t1.CO_EMPR, t1.CO_MONE, t1.FE_REGI
				 From	TDCONC_FLUJ t1
				 Where 	t1.CO_EMPR = @ISCO_EMPR And 
						t1.CO_MONE != @ISCO_MONE  And
						t1.FE_REGI >= @IDFE_PROY
				 Group By t1.CO_EMPR, t1.CO_MONE, t1.FE_REGI ) t2
	Where
		t1.CO_EMPR = @ISCO_EMPR And 
		t1.CO_MONE != @ISCO_MONE And
		t1.FE_REGI >= @IDFE_PROY And
		t1.CO_EMPR = t2.CO_EMPR And
		t1.CO_MONE = t2.CO_MONE And
		t1.FE_REGI = t2.FE_REGI And 
		Not Exists ( Select t3.CO_EMPR
					From TDCONC_FLUJ t3, 
								(Select	Top 10	t1.CO_EMPR, t1.FE_REGI
								 From	TDCONC_FLUJ t1
								 Where 	t1.CO_EMPR = @ISCO_EMPR And 
										t1.CO_MONE = @ISCO_MONE  And
										t1.FE_REGI >= @IDFE_PROY
								 Group By t1.CO_EMPR, t1.FE_REGI ) t2
					Where
						t3.CO_EMPR = @ISCO_EMPR And 
						t3.CO_MONE = @ISCO_MONE And
						t3.FE_REGI >= @IDFE_PROY And
						t3.CO_EMPR = t2.CO_EMPR And
						t3.FE_REGI = t2.FE_REGI And
						t3.CO_EMPR = t1.CO_EMPR And
						t3.FE_REGI = t1.FE_REGI )
		)	


		Insert Into #TWCONC_FLUJ_DIAS (
		CO_EMPR,	FE_DETA_DIGI,	NU_ANNO,	NU_MESE,	DE_DIAS,	NU_DIAS, ST_ORIG)
		(Select	Top 10	t1.CO_EMPR, t1.FE_REGI, Year(t1.FE_REGI), Month (t1.FE_REGI) ,
			 dbo.FN_DESC_DIAS_Q01(DATEPART ( dw , t1.FE_REGI )) + '_' + Convert(Varchar(2),DatePart(Day, 
t1.FE_REGI)), 
			RTrim(Ltrim(Convert(Varchar(3),IsNull(DatePart ( dayofyear , t1.FE_REGI ), 0)))),
			'3'
		 From	TDCONC_FLUJ t1
		 Where 	t1.CO_EMPR = @ISCO_EMPR And 
				t1.CO_MONE = @ISCO_MONE  And
				t1.FE_REGI >= @IDFE_PROY
		 Group By t1.CO_EMPR, t1.FE_REGI
		 Union all
		Select	Top 10	t1.CO_EMPR, t1.FE_REGI, Year(t1.FE_REGI), Month (t1.FE_REGI) ,
			 dbo.FN_DESC_DIAS_Q01(DATEPART ( dw , t1.FE_REGI ))  + '_' + Convert(Varchar(2),DatePart(Day, 
t1.FE_REGI)), 
			RTrim(Ltrim(Convert(Varchar(3),IsNull(DatePart ( dayofyear , t1.FE_REGI ), 0)))),
			'3'
		 From	TDCONC_FLUJ t1
		 Where 	t1.CO_EMPR = @ISCO_EMPR And 
				t1.CO_MONE != @ISCO_MONE  And
				t1.FE_REGI >= @IDFE_PROY  And
				Not Exists ( Select t2.FE_REGI
						   From TDCONC_FLUJ t2
						   Where  t2.CO_EMPR = t1.CO_EMPR And
								t2.CO_MONE = @ISCO_MONE And
								t2.FE_REGI = t1.FE_REGI 
							)
		 Group By t1.CO_EMPR, t1.FE_REGI )
			
--- *** ELIMINAMOS DE LA TABLA DE TRABAJO (BACKUP)   *** ---
	Delete #TWCONC_FLUJ

--- *** INSERTAMOS EN LA TABLA DE TRABAJO (BACKUP)   *** ---
	Select @VDFE_MINI_DIAS = Min(FE_DETA_DIGI)
	From 	#TWCONC_FLUJ_DIAS
	Where CO_EMPR = @ISCO_EMPR 

		---- Saldo Inicial ----
		Insert Into #TWCONC_FLUJ
			(CO_EMPR, TI_CLIE, DE_SUBR, FE_PROY_EJEC, 
			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )
		Values(
			@ISCO_EMPR, '000' , 'SALDO INICIAL', @IDFE_PROY,
			@ISCO_MONE, @VDFE_MINI_DIAS, '0', 
			0 , 99.99 )

		---- Clientes ----
		Insert Into #TWCONC_FLUJ
			(CO_EMPR, TI_CLIE, DE_SUBR, FE_PROY_EJEC, 
			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )
		(
		Select 
			CO_EMPR, IsNull(TI_CLIE,'111'), Max(DE_TIPO_CLIE), Max(@IDFE_PROY), 
			Max(@ISCO_MONE), FE_PROY_EJEC, Max(ST_EJEC),
			0, IsNull(Sum(IsNull(IM_CONV,0)),0)
		From 	#TWCONC_FLUJ_Q01_1,  TTTIPO_CLIE
		Where ST_EJEC = '1' And
		#TWCONC_FLUJ_Q01_1.TI_CLIE = TTTIPO_CLIE.CO_TIPO_CLIE
		Group By CO_EMPR, TI_CLIE , FE_PROY_EJEC
		)	

		---- Proveedores  ----
		Insert Into #TWCONC_FLUJ
			(CO_EMPR, TI_CLIE, DE_SUBR, FE_PROY_EJEC, 
			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )
		(
		Select 
			CO_EMPR, IsNull(TI_CLIE,'222'), Max('PROVEEDORES'), Max(@IDFE_PROY), 
			Max(@ISCO_MONE), FE_PROY_EJEC, Max(ST_EJEC),
			0, IsNull(Sum(IsNull(IM_CONV,0)),0)
		From 	#TWCONC_FLUJ_Q01_1
		Where ST_EJEC = '2'
		Group By CO_EMPR, TI_CLIE , FE_PROY_EJEC

		)	

		---- Conceptos Adicionales ----
		Insert Into #TWCONC_FLUJ
			(CO_EMPR, TI_CLIE, DE_SUBR, FE_PROY_EJEC, 
			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )
		(
		Select 
			#TWCONC_FLUJ_Q01_1.CO_EMPR, IsNull(TI_CLIE,'333'), Max(DE_CONC_FLUJ), Max(@IDFE_PROY), 
			Max(@ISCO_MONE), FE_PROY_EJEC, Max('2'),
			0, IsNull(Sum(IsNull(IM_CONV,0)),0)
		From 	#TWCONC_FLUJ_Q01_1,  TTCONC_FLUJ
		Where ST_EJEC = '3' And
		#TWCONC_FLUJ_Q01_1.CO_EMPR = TTCONC_FLUJ.CO_EMPR And
		#TWCONC_FLUJ_Q01_1.TI_CLIE = TTCONC_FLUJ.CO_CONC_FLUJ
		Group By #TWCONC_FLUJ_Q01_1.CO_EMPR, TI_CLIE , FE_PROY_EJEC )


--- *** DECLARACION DE VARIABLES PARA EL CURSOR CU_TTARUB_FINA_Q01_1 *** ---
	Declare CU_TDCONC_FLUJ_Q01_1 CURSOR FOR
	Select	Distinct Top 10 
		CO_EMPR,	FE_DETA_DIGI,	NU_DIAS, DE_DIAS
	From 	#TWCONC_FLUJ_DIAS
	Where CO_EMPR = @ISCO_EMPR 
	Order By  CO_EMPR , FE_DETA_DIGI

	select @VSDE_SELE_DIAS = ''

--- *** CURSOR GENERAR DIAS EN BLANCO CON CADA UNO DE LOS RUBROS *** ---
	Open CU_TDCONC_FLUJ_Q01_1
	Fetch CU_TDCONC_FLUJ_Q01_1 INTO @CSCO_EMPR, @CDFE_DETA_DIGI, @CSNU_DIAS, @CSDE_DIAS
	WHILE @@Fetch_status = 0
	Begin
		Select @VSDE_SELE_DIAS = @VSDE_SELE_DIAS + 
			'Sum (PatIndex("' + @CSNU_DIAS +  
'",RTrim(Ltrim(Convert(Varchar(3),IsNull(DatePart(dayofyear,FE_DETA_DIGI),0)))))*IsNull(IM_EJEC,0)) As ' + @CSDE_DIAS + ', 
'
		
		Fetch CU_TDCONC_FLUJ_Q01_1 INTO @CSCO_EMPR, @CDFE_DETA_DIGI, @CSNU_DIAS, @CSDE_DIAS
	End
	Close CU_TDCONC_FLUJ_Q01_1
	Deallocate CU_TDCONC_FLUJ_Q01_1

Select @VSFE_PROY = Convert( Varchar(10), @IDFE_PROY, 103)


--- *************************************************** ---
--- *** EJECUTAMOS EL SELECT QUE CARGARA LA TABLA   *** ---
--- *************************************************** ---
/*
Print '
Select	#TWCONC_FLUJ.TI_CLIE,
		Max(#TWCONC_FLUJ.DE_SUBR), 
		#TWCONC_FLUJ.FE_PROY_EJEC, 
		#TWCONC_FLUJ.ST_DIGI_PROY,
		Max(IsNull(#TWCONC_FLUJ.IM_SALD,0)) AS IM_SALD_ANTE,
		Sum(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0)),
		RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))),
		dbo.FN_DESC_DIAS_Q01(DATEPART ( dw , FE_DETA_DIGI ) ), ' +
		@VSDE_SELE_DIAS		
		+' DateName (month, FE_DETA_DIGI )		
From 	#TWCONC_FLUJ 
Where	#TWCONC_FLUJ.CO_EMPR =  "' + @ISCO_EMPR + '"  And 
		#TWCONC_FLUJ.FE_PROY_EJEC = "' + @VSFE_PROY  + '" 
Group BY  #TWCONC_FLUJ.ST_DIGI_PROY, #TWCONC_FLUJ.TI_CLIE, #TWCONC_FLUJ.FE_PROY_EJEC , #TWCONC_FLUJ.FE_DETA_DIGI
OrDer BY #TWCONC_FLUJ.ST_DIGI_PROY, #TWCONC_FLUJ.TI_CLIE, #TWCONC_FLUJ.FE_PROY_EJEC , #TWCONC_FLUJ.FE_DETA_DIGI

' 

Exec ( '
Select	#TWCONC_FLUJ.TI_CLIE,
		Max(#TWCONC_FLUJ.DE_SUBR), 
		#TWCONC_FLUJ.FE_PROY_EJEC, 
		#TWCONC_FLUJ.ST_DIGI_PROY,
		Max(IsNull(#TWCONC_FLUJ.IM_SALD,0)) AS IM_SALD_ANTE,' +
		@VSDE_SELE_DIAS
		+'
		Sum(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
From 	#TWCONC_FLUJ 
Where	#TWCONC_FLUJ.CO_EMPR =  "' + @ISCO_EMPR + '"  And 
		#TWCONC_FLUJ.FE_PROY_EJEC = "' + @VSFE_PROY  + '" 
Group BY  #TWCONC_FLUJ.ST_DIGI_PROY, #TWCONC_FLUJ.TI_CLIE, #TWCONC_FLUJ.FE_PROY_EJEC
OrDer BY #TWCONC_FLUJ.ST_DIGI_PROY, #TWCONC_FLUJ.TI_CLIE, #TWCONC_FLUJ.FE_PROY_EJEC 

' )

*/

End
/********************************************** F I N ********************************************************/


GO
